Page({
  data: {
    dialogVisible: false,
    houseList: [],
    isEmpty: false
  },
  onShow() {
    this.getHouseList()
  },
  async getHouseList() {
    const res = await wx.http.get('/room')
    if (res.code !== 10000) return wx.utils.toast()
    this.setData({
      houseList: res.data,
      isEmpty: res.data.length === 0
    })
  },

  swipeClose(ev) {
    const {
      position,
      instance
    } = ev.detail

    if (position === 'right') {
      // 显示 Dialog 对话框
      this.setData({
        dialogVisible: true,
      })
      this.houseId = ev.mark.id
      // swiper-cell 滑块关闭
      instance.close()
    }
  },
  // 关闭dialog
  dialogClose(e) {
    console.log(e);
    if (e.detail === 'confirm') this.deleteHouse()
  },
  // 删除房屋
  async deleteHouse() {
    console.log(this.houseId);
    const res = await wx.http.delete('/room/' + this.houseId)
    if (res.code !== 10000) return wx.utils.toast('删除失败')
    // 删除成功重新掉一下列表接口
    this.getHouseList()
  },
  goDetail(e) {
    wx.navigateTo({
      url: '/house_pkg/pages/detail/index?id=' + e.mark.id,
    })
  },

  addHouse() {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index',
    })
  },
})